package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


/**
 * 报警数据Service业务层处理
 *
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return alertDataMapper.deleteById(id);
    }

    //=======================================================================================

    @Autowired
    TokenService tokenService;

    /**
     * 报警数据列表
     *
     * @param alertDataDto
     * @return
     */
    @Override
    public TableDataInfo<AlertData> alertDataList(AlertDataDto alertDataDto) {

        Page<AlertData> page = new Page<>(alertDataDto.getPageNum(), alertDataDto.getPageSize());

        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(alertDataDto.getDeviceName() != null, AlertData::getDeviceName, alertDataDto.getDeviceName());
        queryWrapper.eq(alertDataDto.getStatus() != null, AlertData::getStatus, alertDataDto.getStatus());
        queryWrapper.between(alertDataDto.getStartTime() != null && alertDataDto.getEndTime() != null, AlertData::getCreateTime, alertDataDto.getStartTime(), alertDataDto.getEndTime());

        // 获取当前登录用户的userId
        Long currentUserId = UserThreadLocal.getUserId();
        System.out.println("当前登录用户的userId为：" + currentUserId);
        queryWrapper.eq(AlertData::getUserId, currentUserId);

        page = page(page, queryWrapper);

        TableDataInfo<AlertData> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setCode(200);

        return tableDataInfo;
    }


    @Autowired
    private ISysUserService sysUserService;

    /**
     * 处理报警数据
     *
     * @param alertData
     */
    @Override

    public void handleAlertData(AlertData alertData) {

        // 获取所要处理的数据
        AlertData data = alertDataMapper.selectOne(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getId, alertData.getId()));


        // 获取当前登录用户的userId，并从中获取到用户名
        Long currentUserId = UserThreadLocal.getUserId();
        SysUser sysUser = sysUserService.selectUserById(currentUserId);
        String userName = sysUser.getUserName();

        LambdaQueryWrapper<AlertData> queryWrapper = Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getCreateTime, data.getCreateTime());

        alertData.setProcessorName(userName);
        alertData.setStatus(1);

        alertDataMapper.update(alertData, queryWrapper);
    }
}
